import { defineStore } from 'pinia';
import axios from 'axios';

interface Service {
  id: string;
  name: string;
  namespace: string;
  ip: string;
  port: number;
  status: 'active' | 'inactive' | 'error';
  lastSeen: string;
  metrics: {
    requestCount: number;
    errorRate: number;
    avgResponseTime: number;
  };
}

export const useServicesStore = defineStore('services', {
  state: () => ({
    services: [] as Service[],
    loading: false,
    error: null as string | null,
    selectedService: null as Service | null
  }),
  
  getters: {
    getServiceById: (state) => (id: string) => {
      return state.services.find(service => service.id === id);
    },
    activeServices: (state) => {
      return state.services.filter(service => service.status === 'active');
    },
    serviceCount: (state) => {
      return state.services.length;
    }
  },
  
  actions: {
    async fetchServices() {
      this.loading = true;
      this.error = null;
      
      try {
        // 替换为实际的API端点
        const response = await axios.get('/api/services');
        this.services = response.data;
      } catch (err: any) {
        this.error = err.message || '获取服务列表失败';
        console.error('获取服务列表失败:', err);
      } finally {
        this.loading = false;
      }
    },
    
    selectService(service: Service) {
      this.selectedService = service;
    },
    
    clearSelectedService() {
      this.selectedService = null;
    }
  }
});
